//
//  SLPageNormalContentView.m
//  ToolsLibrary
//
//  Created by 王胜利 on 2018/11/13.
//  Copyright © 2018 Javor Feng. All rights reserved.
//  一并初始化并加载所要显示的所有View，(使用于固定的View)
//

#define WEAK(type)   __weak typeof(type) weak##type = type;

#import "SLPageNormalContentView.h"
#import "SLPageBaseScrollView.h"
#import "SLPageManager.h"
#import "Masonry.h"
//#import "ThemeKit.h"

@interface SLPageNormalContentView()<UIScrollViewDelegate>

@property (nonatomic, strong) SLPageBaseScrollView *scrollView;

@end

@implementation SLPageNormalContentView

@synthesize childViews = _childViews;
@synthesize currentIdx = _currentIdx;
@synthesize didScrollToIdx = _didScrollToIdx;


- (instancetype)init{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setChildViews:(NSArray<UIView *> *)childViews{
    _childViews = childViews;
    [self layoutIfNeeded];
    [self.scrollView layoutIfNeeded];
    [self addChildViews];
}

/// MARK: - 添加子视图
- (void)addChildViews{

    for (int i = 0; i < self.childViews.count; i ++) {
        UIView *childView = self.childViews[i];
        UIView *lastView;
        if (self.scrollView.subviews.count > 0) {
            lastView = self.scrollView.subviews.lastObject;
        }
        
        [self.scrollView addSubview:childView];
        [childView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.centerY.bottom.equalTo(self.scrollView);
            make.width.equalTo(@(self.bounds.size.width));
            if (lastView) {
                make.left.equalTo(lastView.mas_right);
            }else{
                make.left.equalTo(self.scrollView);
            }
            /// 最后一个添加距右的约束
            if (i == self.childViews.count-1){
                make.right.equalTo(self.scrollView);
            }
        }];
    }
}

- (void)scrollToIndex:(NSInteger)idx animated:(BOOL)animated{
    self.currentIdx = idx;
    [self layoutIfNeeded];
    if (animated) {
        WEAK(self)
        [UIView animateWithDuration:0.25 animations:^{
            weakself.scrollView.contentOffset = CGPointMake(weakself.bounds.size.width*idx, 0);
        }];
    }else{
        self.scrollView.contentOffset = CGPointMake(self.bounds.size.width*idx, 0);
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.scrollView layoutIfNeeded];
    self.scrollView.contentSize = CGSizeMake(self.bounds.size.width*self.childViews.count, 0);
}


#pragma mark - UIScrollViewDeleagte
// 滚动中
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    scrollView.scrollEnabled = !(scrollView.contentOffset.x < 0);
    NSInteger idx = (scrollView.contentOffset.x + self.bounds.size.width/2)/self.bounds.size.width;
    if (idx == self.currentIdx) {
        return;
    }
    self.currentIdx = idx;
    if (self.didScrollToIdx) self.didScrollToIdx(idx);
}

#pragma mark - Lazyload
- (SLPageBaseScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[SLPageBaseScrollView alloc]init];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = YES;
        [self addSubview:_scrollView];
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return _scrollView;
}

@end
